package com.cg.asm.core.controller.login;

import com.alibaba.fastjson.JSONObject;
import com.cg.asm.core.domain.vo.user.UserUpPwdVO;
import com.cg.asm.core.service.user.IUserInfoService;
import com.cg.asm.core.util.AsmApiUtils;
import com.cg.asm.client.CommonUtil;
import com.cg.asm.client.exception.AsmException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "LoginController", value = "登录接口")
@RestController
@RequestMapping(value = "/login")
public class LoginController {

    @Resource
    SessionRegistry sessionRegistry;
    @Resource
    private IUserInfoService userInfoService;

    /**
     * 点击退出并退出成功时进入
     * 返回退出成功标示
     *
     * @return
     */
    @ApiOperation(value = "点击退出并退出成功时进入此方法", notes = "退出")
    @GetMapping("/loginOutSuccess")
    public JSONObject loginOutSuccess() {
        return CommonUtil.successJson("成功退出");
    }

    /**
     * 前端Router中配制调用此方法
     *
     * @return
     */
    @ApiOperation(value = "前端Router中配制调用此方法,此方法在Security管控范围内，能正常进入则表示用户有效", notes = "登录")
    @GetMapping("/router")
    public JSONObject router() {
        return CommonUtil.successJson(true);
    }

    /**
     * 获取Session中的用户信息
     *
     * @return
     */
    @ApiOperation(value = "获取Session中的用户信息", notes = "登录")
    @GetMapping("/sessionUserInfoByAsm")
    public JSONObject sessionUserInfoByAsm(HttpServletRequest request) {
        return AsmApiUtils.getCurrentUserInfo(sessionRegistry, request.getSession().getId());
    }

    @ApiOperation(value = "修改密码", notes = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userUpPwdVO", value = "用户修改密码对象", required = true, dataType = "UserUpPwdVO", paramType = "body")
    })
    @PostMapping("/updatePWD")
    public JSONObject updatePWD(UserUpPwdVO userUpPwdVO) {
        try {
            userInfoService.updatePWD(userUpPwdVO);
            return CommonUtil.successJson();
        } catch (AsmException e) {
            return CommonUtil.errorJson(e.getMessage());
        }
    }
}
